<?php

namespace app\admin\controller;

use think\Request;
use think\Db;
use think\Controller;
use think\Loader;

class Student extends Controller {

    public function index(){
        return $this->fetch();
    }
    /**
     * 后台首页
     */
    public function dologin(){
        //echo "test";
        //if (isset($_POST['username'])) {
        if (request()->isPost()){
            $name = input('post.username');
            $num = input('post.num');
            $password = input('post.password');
            $verifycode = input('post.verifycode');

            if (!$name) {
                $this->error('姓名不能为空');
            }
            if (!$num) {
                $this->error('学号不能为空');
            }
            if (!$password) {
                $this->error('密码不能为空');
            }
            if(!captcha_check($verifycode)){
			    $this->error('验证码错误');
		    }

            $result = Db::table('ticket_student')->where('num',$num)->find();
            $password = md5(md5($password));
            if($result['password'] != $password){
                $this->error('信息输入错误');
            }
            if($result['name'] != $name){
                $this->error('信息输入错误');
            }
            if($result['get_ticket'] != 0){
                $ticket_ret = $result['ticket'];
                echo "您的票号为：";
                echo $ticket_ret;
            }
            else{
                $ticket_res = Db::table('ticket_show')->find();
                if($ticket_res['is_start'] == 0){
                    $this->error('还未开始放票');
                }
                $ticket_num = $ticket_res['last_num'];
                if($ticket_num == 0){
                    $this->error('余票为0');
                }
                $ticket_num = $ticket_num - 1;
                $ticket_ret = md5($ticket_res['time'].$ticket_num.$name.rand(1000,9999));
                $this->assign("ticket_ret", $ticket_ret);
                echo "您的票号为：";
                echo $ticket_ret;

                Db::table('ticket_show')->where('id',1)->update(['last_num'=>$ticket_num]);
                Db::table('ticket_student')->where('name',$name)->update(['get_ticket'=>1, 'ticket'=>$ticket_ret]);
                Db::table('ticket_show_list')->insert(['show_id'=>1, 'student_id'=>$result['id']]);
            }
        }
        else{
            $this->error('申请失败');
        }
    }

    public function test(){
        echo "test";
    }

}